﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TravelAgencyDAL;

namespace Beginning
{
    public partial class frmEditWayStation : Form
    {
        public long wayStationID
        {
            get;
            set;
        }

        public long destID
        {
            get;
            set;
        }

        public frmEditWayStation(long wayStationID, long destID)
        {
            InitializeComponent();
            this.wayStationID = wayStationID;
            this.destID = destID;
            BindControls();
        }

        private void BindControls()
        {
            var wayStationName = from ws in DAO.GetAllWayStationForDestination(destID)
                                 where ws.WayStationID == wayStationID
                                 select ws.Settlement.SettlementName;

            txtWayStationName.Text = wayStationName.First();

            var wayStationPriceTo = from w in DAO.GetAllWayStationForDestination(destID)
                                    where w.WayStationID == wayStationID                                   
                                    select w.WayStationPriceTo;

            txtPriceTo.Text = wayStationPriceTo.First().ToString("F");

            var wayStationStay = from w in DAO.GetAllWayStationForDestination(destID)
                                 where w.WayStationID == wayStationID
                                 select w.WayStationStay;

            txtStay.Text = wayStationStay.First().ToString();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            frmDestinationDetails details = new frmDestinationDetails(destID);
            details.Show();
            this.Hide();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            bool confirm =
                MessageBox.Show("Submit these changes?", "Confirmation", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes;
            if (confirm)
            {
                var currentWayStation = (from ws in DAO.GetAllWayStationForDestination(destID)
                                         where ws.WayStationID == wayStationID
                                         select ws).First();

                currentWayStation.WayStationPriceTo = Convert.ToDecimal(txtPriceTo.Text);
                currentWayStation.WayStationStay = Convert.ToInt32(txtStay.Text);

                DAO.Update();

                MessageBox.Show("The new data saved!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
                frmDestinationDetails details = new frmDestinationDetails(destID);
                details.Show();
                this.Hide();
            }
        }
        
        private void frmEditWayStation_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmDestinationDetails details = new frmDestinationDetails(destID);
            details.Show();
            this.Hide();
        }

        #region Validating

        private void txtPriceTo_Validating(object sender, CancelEventArgs e)
        {
            ValidatePriceTo();
        }

        private void txtStay_Validating(object sender, CancelEventArgs e)
        {
            ValidateStay();
        }

        private bool ValidatePriceTo()
        {
            if (IsPriceToValid(txtPriceTo.Text))
            {
                epValidateData.SetError(txtPriceTo, "");
                return true;
            }
            else
            {
                epValidateData.SetError(txtPriceTo, "Not a valid price!");
                return false;
            }
        }

        private bool ValidateStay()
        {
            if (IsPriceToValid(txtStay.Text))
            {
                epValidateData.SetError(txtStay, "");
                return true;
            }
            else
            {
                epValidateData.SetError(txtStay, "Not valid minutes for stay!");
                return false;
            }
        }

        private bool IsPriceToValid(string price)
        {
            try
            {
                if (Decimal.Parse(price) > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (FormatException)
            {
                return false;
            }
        }

        #endregion
    }
}
